import {createRouter, createWebHashHistory} from 'vue-router'
import Cookies from 'js-cookie'

const loginPage = () =>  import( "@/views/login/index.vue")
const indexPage = () =>  import( "@/views/index/index.vue")
const casePage = () =>  import( "@/views/index/case.vue")

const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
            path: "/",
            name: "index",
            component: indexPage,
            meta: {
                requireAuth: true,
            }
        },{
            path: "/login",
            name: "login",
            component: loginPage,
        },{
            path: '/case',
            name: "case",
            component: casePage,
            meta: {
                requireAuth: true,
            }
        },{
            path: '/:pathMatch(.*)',
            redirect: '/'
        }
    ]
})

router.beforeEach((to, from, next) => {
    const token = Cookies.get('loginToken')
    console.log(token)
    if (to.matched.some(record => record.meta.requireAuth) && !token) {
        next({ name: 'login' })
    } else {
        next()
    }
  })

export default router;